/**
 * Created by V5470-I5 on 2016/5/6.
 */
import java.net.*;
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;

public class Test5 {
    /**

     * @param args the command line arguments

     */

    public static void main(String args[]) {
        ServerSocket sc = null;
        ServerSocket sc1 = null;
        Socket socket=null;
        Socket socket1=null;
        try {
            sc= new ServerSocket(1105);
            sc1= new ServerSocket(1106);
            System.out.println("端口号:" + sc.getLocalPort());
            System.out.println("服务器1已经启动...");
            System.out.println("端口号:" + sc1.getLocalPort());
            System.out.println("服务器2已经启动...");
            socket = sc.accept();
            System.out.println("已经建立连接");
            socket1 = sc1.accept();
            System.out.println("已经建立1连接");

            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            BufferedReader in1 = new BufferedReader(new InputStreamReader(socket1.getInputStream()));
            System.out.print("已接收\n");

            PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
            String   aline=in.readLine();
            String   aline1=in1.readLine();

            System.out.print("写入文件中...\n");
            byte [] bytes = aline.getBytes("GB2312");
            byte [] bytes1 = aline1.getBytes("GB2312");

            aline = new String(bytes, "GB2312");
            aline1 = new String(bytes1, "GB2312");
            FileOutputStream  s=new FileOutputStream("key1.dat");
            s.write(bytes);

            FileOutputStream s1=new FileOutputStream("SEnc.dat");

            s1.write(bytes1);

            System.out.print("已写入文件\n");

            FileInputStream f=new FileInputStream("key1.dat");

            ObjectInputStream b=new ObjectInputStream(f);

            Key k=(Key)b.readObject( );

            byte[ ] kb=k.getEncoded( );

            FileOutputStream  f2=new FileOutputStream("keykb1.dat");

            f2.write(kb);

            System.out.print("生成解密密钥\n");

            for(int i=0;i<kb.length;i++){

            }
            FileInputStream a=new FileInputStream("SEnc.dat");

            int num=a.available();

            byte[ ] ctext=new byte[num];

            a.read(ctext);

            FileInputStream  f1=new FileInputStream("keykb1.dat");

            int num2=f1.available();

            byte[ ] keykb=new byte[num2];

            f1.read(keykb);
            SecretKeySpec e=new  SecretKeySpec(keykb,"DESede");
            Cipher cp=Cipher.getInstance("DESede");

            cp.init(Cipher.DECRYPT_MODE, k);

            byte []ptext=cp.doFinal(ctext);

            String p=new String(ptext,"GB2312");

            System.out.print("解密中...\n");

            System.out.println(p);
            System.out.println("从客户端接收到信息为："+p);

            out.println(p);

            out.close();

            in.close();

            sc.close();

        } catch (Exception e) {

            System.out.println(e);

        }

    }

}
